package com.gdue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdue.commons.R;
import com.gdue.dao.DishMapper;
import com.gdue.dto.DishDto;
import com.gdue.entity.Dish;
import com.gdue.entity.DishFlavor;
import com.gdue.exception.CustomException;
import com.gdue.service.DishFalavorService;
import com.gdue.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish>  implements DishService {

    @Autowired
    private DishFalavorService  dishFalavorService;

    /**
     * 保存新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        this.save(dishDto);

        //当保存完后获取dishId
        Long dishId=dishDto.getId();

        //菜品口味
        List<DishFlavor> flavors=dishDto.getFlavors();
        //给flavors里面的id全部赋值（foreach,stream流）
        flavors=flavors.stream().map((item)->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        dishFalavorService.saveBatch(flavors);
    }

    /**
     * 根据id查询菜品信息和相关口味信息
     * @param id
     * @return
     */
    @Override
    @Transactional
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品信息
        Dish    dish=this.getById(id);

        DishDto dishDto=new DishDto();
        //对象拷贝
        BeanUtils.copyProperties(dish,dishDto);

        //查询对应的口味信息
        LambdaQueryWrapper<DishFlavor>  lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> list = dishFalavorService.list(lambdaQueryWrapper);

        dishDto.setFlavors(list);

        return dishDto;
    }

    /**
     * 修改菜品，同时修改菜品信息和相关口味信息
     * @param dishDto
     */
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //更新菜品信息
        this.updateById(dishDto);
        //删除相关口味信息
        LambdaQueryWrapper<DishFlavor>  lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFalavorService.remove(lambdaQueryWrapper);
        //添加修改的口味信息
        List<DishFlavor> flavors=dishDto.getFlavors();
        flavors=flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFalavorService.saveBatch(flavors);

    }

    /**
     * (批量)删除菜品，同时删除相应的口味
     * @param ids
     */
    @Override
    @Transactional
    public void deleteWithFlavor(List<Long> ids) {
        //判断菜品能否删除（是否启售）
        LambdaQueryWrapper<Dish>    lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Dish::getId,ids);
        lambdaQueryWrapper.eq(Dish::getStatus,1);
        int count=this.count(lambdaQueryWrapper);
        if (count>0){
            throw new CustomException("菜品正在销售，不能删除");
        }
        //删除菜品
        this.removeByIds(ids);
        //删除相应的口味
        LambdaQueryWrapper<DishFlavor>  dishFlavorLambdaQueryWrapper=new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.in(DishFlavor::getDishId,ids);
        dishFalavorService.remove(dishFlavorLambdaQueryWrapper);
    }

    /**
     * 修改菜品销售状态
     * @param status
     * @param ids
     */
    @Override
    public void updateStatusByIds(int status, List<Long> ids) {

        UpdateWrapper<Dish> updateWrapper=new UpdateWrapper<>();
        updateWrapper.in("id",ids);
        updateWrapper.set("status",status);

        this.update(null,updateWrapper);

    }


}
